package com.iflytek.aichang.tv.adapter;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.iflytek.aichang.util.ReflectHelper;
import com.iflytek.log.Logger;

import android.R.integer;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class BaseListAdapter<Holder, Item> extends BaseAdapter {
    protected List<Item> mItemList = new ArrayList<Item>();
	protected LayoutInflater mLayoutInflater;
	protected int mItemLayout;
	protected Context mContext;
	private Class<Holder> mHolderClass;

	@Target(ElementType.FIELD)
	@Retention(RetentionPolicy.RUNTIME)
	public @interface HolderViewId {
		int value() default 0;
		String resId();
	}

	public BaseListAdapter(Context context, Class<Holder> cls, int itemLayout, List<Item> items) {
		if (context == null) {
			return;
		}
		mLayoutInflater = LayoutInflater.from(context);
		if (items != null) {
			mItemList.addAll(items);
		}
		mItemLayout = itemLayout;
		mContext = context;
		mHolderClass = cls;
	}

	public BaseListAdapter(Context context, Class<Holder> cls, int itemLayout) {
		this(context, cls, itemLayout, null);
	}

	public void clear() {
		mItemList.clear();
		notifyDataSetChanged();
	}

	public void addItem(Item item) {
		mItemList.add(item);
		notifyDataSetChanged();
	}

	public void addList(List<Item> items) {
		mItemList.addAll(items);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mItemList.size();
	}

	@Override
	public Item getItem(int position) {
		return mItemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public Holder initViewHodler(View convertView) {
		Holder holder = ReflectHelper.newInstance(mHolderClass);
		HashMap<String, Field> fieldMap = ReflectHelper.getFields(holder);
		for (Field field : fieldMap.values()) {
			HolderViewId holderViewId = field.getAnnotation(HolderViewId.class);
			if (holderViewId != null) {
				try {
					field.setAccessible(true);
					if (holderViewId.value() != 0) {
						field.set(holder, convertView.findViewById(holderViewId.value()));
					} else if (!TextUtils.isEmpty(holderViewId.resId())) {
						int id = ReflectHelper.getResourseIdByName(mContext.getApplicationContext().getPackageName(),
						        holderViewId.resId());
						field.set(holder, convertView.findViewById(id));
					}
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					Logger.log().e(
					        "class:" + this.getClass().getSimpleName() + "initViewHolder error:"
					                + "findviewById Type not match" + "view:" + field.getName() + "view id:"
					                + holderViewId.value());
					e.printStackTrace();
				}
			}
		}
		return holder;
	}

	abstract public void setViewHodler(Holder viewHolder, int position);

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder viewHolder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(mItemLayout, null);
			viewHolder = initViewHodler(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (Holder) convertView.getTag();
		}
		setViewHodler(viewHolder, position);
		return convertView;
	}
}
